<?php

/**
 * @filename Login.php
 * @encoding UTF-8
 *
 * @author 徐军
 *
 * @link http://www.okd100.com
 *
 * @copyright 南昌欧科达科技有效公司
 * @datetime 2016-7-18  13:44:21
 *
 * @version 1.0
 * @Description
 */

namespace app\admin\controller;

use app\common\model\Mail as MailModel;
use app\common\model\User as UserModel;
use app\common\model\Admin as AdminModel;

class Index extends Common {

    /**
     * 管理登陆.
     *
     * @return $this|\json|void
     */
    public function Login() {

        try {
            if (IS_POST) {
               // $config_sys = cache('config_sys');


                $data['user_name'] = input('username');
                $data['password'] = input('password');
                $data['code'] = input('code');


                $result = \app\common\model\Admin::where('user_name', $data['user_name'])->select();






                //验证 验证码
                $login_error_count = session('login_error_count');


                if ($login_error_count && $login_error_count >= 3) {
                    if (captcha_check($data['code'], 1) != true) {
                        return error('验证码错误请重新输入！', 'checkCode');
                    }
                }



                if ($result[0]->user_id) {
                    $password = $result[0]->checkPassword($data['password']);

                    if ($password) {

                                            if ($result[0]->user_status == "冻结" || $result[0]->user_status == "已删除")
                        return error('该账号' . $result[0]->user_status . "无法登录！请联系管理人员", 'updateCode');

//
//                        if ($config_sys['system_close'] == 0) {
//
//                            //  if (is_array($result[0]->user_type, array("user", 'agent', 'superuser')))
//                            if ($result[0]->user_type == "user" || $result[0]->user_type == "agent")
//                                return error($config_sys['system_close_prompt'], 'aa');
//                        }


                        $data = $result[0];

                        $data['is_login'] = true;

                        session('user', $data);
                        session('user_id', $result[0]->user_id);

                        cache('session_id' . $result[0]->user_id, session_id());


                        return success('登录成功');
                    } else {
                        session('login_error_count', $login_error_count + 1);

                        return error('账号或密码错误，请重新输入!', 'updateCode');
                    }
                } else {

                    //登录错误3次 提示输入验证码
                    $login_error_count = session('login_error_count');


                    if ($login_error_count && $login_error_count >= 2) {
                        return error('账号或密码错误，请重新输入!', 'checkCode');
                    }
                    session('login_error_count', $login_error_count + 1);

                    return error('账号或密码错误，请重新输入!', 'updateCode');
                }
                // return success('登录成功');
            } else {
                return view();
            }
        } catch (\Exception $exc) {
            echo $exc->getMessage();
        }
    }

    /**
     * 后台管理.
     *
     * @return type
     */
    public function index() {
        return $this->fetch('index', ['title' => '后台管理']);
    }

    /**
     * 退出登录.
     */
    public function signOut() {
        $user = session('user');
        cache('session_id' . $user->user_id, null);
        session('user', null);
        session('user_id', null);
        session('login_error_count', 0);
        $this->redirect('Admin/Index/login');
    }

    public function welcome() {
        return view('welcome');
    }

//    public function getunMessageCount() {
//        $usr = UserModel::get(session('user_id'));
//        if ($usr->user_type == 'admin' or $usr->user_type == 'superuser') {
//            $mailcount = MailModel::where('mail_type', 0)->where('is_read', 0)->count();
//        } else {
//            $mailcount = $usr->mails()->where('is_read', 0)->where('mail_status % 2 = 0')->count();
//        }
//        return $mailcount;
//    }

}
